/*
 * Copyright (c) 2022，公司名称
 * All rights reserved.
 * 文件名称：high_uart.c
 * 摘要：高边串口
 * 当前版本：V1.0.0,WCD,2023/1/10,初版
 * 历史版本：Vx.x.x，编写者/修改者，修改时间，修改内容（重大发布时，总结重点变更内容）
 */
#include "high_includes.h"
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓应用层数据定义开始↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
#if 1
static uint8_t resv_buff[UART_LEN] = {0u};
high_uart_rx_struct high_uart_rx_st = {0u};
high_uart_tx_struct high_uart_tx_st = {0u};
static EM_TRUE_FALSE uart_resv_done = FALSE;
static uint8_t revlen = 0U;
static uint8_t revArry[UART_LEN] = {0U};
static uint32_t high_time_tick = 0u;
void high_uart30_rx(void *data);
#endif
/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑应用层数据定义结束↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/
static void uart_cbf()
{
  /* receive data */
  uint8_t ch = UART_ReceiveByte(UART0_ID);
  high_uart30_rx(&ch);
}
void uart_send(uint8_t const *logBuf, uint32_t logLen)
{
  uint32_t i = 0;
  uint8_t ch = 0;
  for (i = 0; i < logLen; i++)
  {
    ch = *(logBuf + i);
    /* Wait untill FIFO is empty */
    while (RESET == UART_GetLineStatus(UART0_ID, UART_LINESTA_TEMT))
      ;

    /* Send data */
    UART_SendByte(UART0_ID, ch);
  }
}
/*
 * 函数介绍: 串口初始化
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void high_uart_clock_init(void)
{
  /*Config UART1 clock, enable UART1 module*/
  CLK_ModuleSrc(CLK_UART0, CLK_SRC_FIRC64M); //   CLK_SRC_OSC40M
  CLK_SetClkDivider(CLK_UART0, CLK_DIV_1);
  SYSCTRL_EnableModule(SYSCTRL_UART0);
}
/*
 * 函数介绍: 串口初始化
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void high_uart_init(void)
{
  /*Uart config struct*/
  static const UART_Config_t uartCfg =
      {
          19200,
          UART_DATABITS_8,
          UART_PARITY_NONE,
          UART_STOPBITS_1,
          DISABLE,
          64000000,
      };
  /*When UART re-open after close, need to check and clear busy status*/
  while (ERR == UART_WaitBusyClear(UART0_ID, 7000))
  {
    (void)UART_ReceiveByte(UART0_ID);
  }
  /* fifo config */
  const UART_FIFOConfig_t fifoCfg =
      {
          ENABLE, ENABLE, ENABLE, UART_TX_FIFO_HALF, UART_RX_FIFO_CHAR_1};
  UART_FIFOConfig(UART0_ID, &fifoCfg);
  /* Inital UART1 */
  UART_Init(UART0_ID, &uartCfg);

  /* callback function */
  UART_InstallCallBackFunc(UART0_ID, UART_INT_RBFI, uart_cbf);
  /* enable received data available interrupt  启用接收数据可用中断*/
  UART_IntMask(UART0_ID, UART_INT_RBFI, UNMASK);
  /* enable IRQ */
  NVIC_EnableIRQ(UART0_IRQn);
  high_time_tick = Get1MsTickVal();
}

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓应用层函数定义开始↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
#if 1
/*
 * 函数介绍: 串口校验函数
 * 参数:buff 源数据指针
 * 参数:len 数据长度
 * 返回值: {*}
 * 备注:
 */
static ErrorStatus uart_check_data(uint8_t *buff, uint8_t len)
{
    uint8_t temp = 0x55u;
    uint8_t i = 0u;
    ErrorStatus res = FAIL;
    len -= 1;

    if (buff != NULL)
    {
        uint8_t check_value = buff[len];

        for (i = 0u; i < len; i++)
        {
            temp ^= buff[i];
            temp++;
        }
        if (temp == check_value)
        {
            res = SUCCESS;
        }
        else
        {
            __NOP();
        }
    }
    else
    { ;
    }
    return res;
}
/*
 * 函数介绍: 串口接收函数
 * 参数: data 数据指针
 * 返回值: {*}
 * 备注:
 */
void high_uart30_rx(void *data)
{
    static uint32_t uart_tick = 0u;
    uint8_t *pData = (uint8_t *) data;
    if (NULL != pData)
    {
        if (1 == Get1MsTickInterval(uart_tick,HIGH_UART30_RX_TIME) )
        {
            revlen = 0u;
        }
        else
        {
        }
        uart_tick = Get1MsTickVal();/* 获取计数值 */

        revArry[revlen] = *pData;
        revlen++;

        if (revlen == UART_LEN)
        {
            uart_resv_done = TRUE;
            memcpy(resv_buff, revArry, UART_LEN);
            revlen = 0U;
#if 0
            uart30.write(&uart30, 0X7F);
            uart30.writes(&uart30, revArry, 8);
#endif
//            uart31.write_mulit_bytes(&uart31, resv_buff, MDL_8_data);
        }
    }
}
/*
 * 函数介绍: 串口数据初始化
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void high_uart_data_init(void)
{
    memset(resv_buff, 0u, UART_LEN);
    memset(&high_uart_rx_st, 0u, sizeof(high_uart_rx_struct));
    memset(&high_uart_tx_st, 0u, sizeof(high_uart_tx_struct));
}
float high_uart30_get_out_temp(void)
{
    float act_temp = high_uart_rx_st.sig.act_temp_deci / 10.0 + high_uart_rx_st.sig.act_temp;
    return act_temp;
}
/*
 * 函数介绍: 生成串口checksum
 * 参数:buff 源数据指针
 * 参数:len 数据长度
 * 返回值: {*}
 * 备注:
 */
static void high_uart_gen_checksum(uint8_t *buff, uint8_t len)
{
    uint8_t temp = 0x55u;
    uint8_t i = 0u;
    if (buff != NULL)
    {
        len -= 1u;

        for (i = 0u; i < len; i++)
        {
            temp ^= buff[i];
            temp++;
        }
        buff[i] = temp;
    }
    else
    {
        Pointer_Null_Error;
    }
}

/*
 * 函数介绍: 串口收发处理
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void high_uart30_rx_tx(void)
{

    /*Rx*/
    if (TRUE == uart_resv_done)
    {
        ErrorStatus check_res = uart_check_data(resv_buff, UART_LEN);
        if (SUCCESS == check_res)
        {
            if (resv_buff[0] != '0')
            {
                //NOP;
            }
            memcpy(high_uart_rx_st.data, resv_buff, UART_LEN);
#ifdef PID_ONLY_POWER
//            high_uart_rx_st.sig.mode = PTC_MODE_POWER;
//            high_uart_rx_st.sig.mode = PTC_MODE_TEMP;
#endif
        }
        else
        { ;
        }
        resv_buff[0] = 0u;
        uart_resv_done = FALSE;
    }
    else
    { ;
    }
    /*Tx*/
    if ( 1 == Get1MsTickInterval(high_time_tick,HIGH_UART30_TX_TIME) ) /* 10mS */
    {
        high_time_tick = Get1MsTickVal(); /* 获取计数值 */
#if 0
        high_uart_tx_st.data[0] = '0';
        high_uart_tx_st.data[1] = '1';
        high_uart_tx_st.data[2] = '2';
        high_uart_tx_st.data[3] = '3';
        high_uart_tx_st.data[4] = '4';
        high_uart_tx_st.data[5] = '5';
        high_uart_tx_st.data[6] = '6';
        high_uart_tx_st.data[7] = '7';
#endif
        /*checksum*/
        high_uart_gen_checksum(high_uart_tx_st.data, UART_LEN);
        //uart30.write_mulit_bytes(&uart30, high_uart_tx_st.data, MDL_8_data);
        uart_send(high_uart_tx_st.data,MDL_8_data);
    }
}

#endif
/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑应用层函数定义结束↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/